package com.example.sharesinfo.ui;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.sharesinfo.R;
import com.example.sharesinfo.base.BaseUIActivity;
import com.example.sharesinfo.bean.PersonalCenterBean;
import com.example.sharesinfo.entity.Constants;
import com.example.sharesinfo.helper.GlideHelper;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import de.hdodenhof.circleimageview.CircleImageView;

public class MyQRCodeActivity extends BaseUIActivity implements View.OnClickListener {

    private ImageButton ib_return;
    private CircleImageView ci_photo;
    private TextView tv_username;
    private ImageView iv_my_code;
    private PersonalCenterBean mUser;

    public static void startActivity(Context mContext, PersonalCenterBean user) {
        Intent intent = new Intent(mContext, MyQRCodeActivity.class);
        intent.putExtra(Constants.INTENT_USER_BEAN, user);
        mContext.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_qrcode);
        initView();
    }

    private void initView() {
        mUser = (PersonalCenterBean) getIntent().getSerializableExtra(Constants.INTENT_USER_BEAN);
        ib_return = findViewById(R.id.ib_return);
        ib_return.setOnClickListener(this);
        ci_photo = findViewById(R.id.ci_photo);
        tv_username = findViewById(R.id.tv_username);
        iv_my_code = findViewById(R.id.iv_my_code);
        GlideHelper.loadUrl(this, mUser.getImageUrl(), ci_photo);
        tv_username.setText(mUser.getUsername());
        createQRCode(String.valueOf(mUser.getId()));
    }

    /**
     * 创建二维码
     */

    private void createQRCode(final String userId) {

        /**
         * View的绘制
         */

        iv_my_code.post(new Runnable() {
            @Override
            public void run() {
                String textContent = "Meet#" + userId;
                Bitmap mBitmap = CodeUtils.createImage(textContent,
                        iv_my_code.getWidth(), iv_my_code.getHeight(), null);
                iv_my_code.setImageBitmap(mBitmap);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.ib_return:
                finish();
                break;
        }
    }
}
